package com.haven.webrobotserver.controller;

import com.alibaba.fastjson.JSONObject;
import com.haven.webrobotserver.domain.User;
import com.haven.webrobotserver.service.LoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@CrossOrigin(origins = "http://localhost:8080")
public class LoginController {

    @Autowired
    private LoginService loginService;

    @PostMapping("/login")
    public JSONObject login(@RequestBody Map<String,String> loginData){

        JSONObject jsonObject = new JSONObject();

        User loginUser = loginService.getLoginUser(loginData.get("username"), loginData.get("password"));

        if(loginUser != null){

            jsonObject.put("msg","登录成功");
            jsonObject.put("status","200");


            jsonObject.put("data",loginUser);
            return jsonObject;

        }

        jsonObject.put("msg","密码或用户名错误");
        jsonObject.put("status","-1");
        jsonObject.put("data","");
        return jsonObject;

    }

}
